<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/style/admin.css" media="all">
</head>

<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">考勤记录</div>
                <div class="layui-card-body">
                    <div style="margin-bottom: 10px;">
                        <div class="layui-input-item" style="float:left;padding-right:10px;">
                            <form class="layui-form">
                                <div class="layui-inline">
                                    <input id="search-user-name" type="text" placeholder="请输入人员姓名"
                                           class="layui-input layui-input-search">
                                </div>
                                <div class="layui-inline">
                                    <input id="search-working-time-greater-than" type="number" min="0"
                                           placeholder="工作时长大于（小时）" class="layui-input layui-input-search">
                                </div>
                                <div class="layui-inline">
                                    <input id="search-working-time-less-than" type="number" min="0"
                                           placeholder="工作时长小于（小时）" class="layui-input layui-input-search">
                                </div>
                                <div class="layui-inline">
                                    <select id="search-state">
                                        <option value="">请选择考勤状态</option>
                                        <option value="1">已签到</option>
                                        <option value="2">已签退</option>
                                    </select>
                                </div>
                                <div class="layui-inline">
                                    <a id="btn-search" class="layui-btn">查找</a>
                                </div>
                            </form>
                        </div>
                        <div style="float:right;">
                            <a id="btn-export" class="layui-btn layui-btn-primary">导出</a>
                            <a id="btn-batch-delete" class="layui-btn layui-btn-danger">删除</a>
                            <a id="btn-refresh" class="layui-btn layui-btn-primary">刷新</a>
                        </div>
                        <div style="clear:both;"></div>
                    </div>
                    <table class="layui-hide" id="sign-table" lay-filter="sign-table"></table>
                    <script type="text/html" id="toolbar-operation">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">详情</a>
                        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="location">位置</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                        {{# if(d.state==1){ }}
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="sign-out">签退</a>
                        {{# } }}
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="sign-form">
    <div class="layui-card">
        <div class="layui-card-header">考勤记录详情</div>
        <div class="layui-card-body">
            <form class="layui-form" action="#" lay-filter="sign-form">
                <input type="hidden" name="id">
                <div class="layui-form-item">
                    <label class="layui-form-label">人员姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="userName" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">签到地址</label>
                    <div class="layui-input-block">
                        <textarea name="locationIn" class="layui-textarea" readonly/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">签到时间</label>
                    <div class="layui-input-block">
                        <input type="text" name="timeIn" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">签退地址</label>
                    <div class="layui-input-block">
                        <textarea name="locationOut" class="layui-textarea" readonly/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">签退时间</label>
                    <div class="layui-input-block">
                        <input type="text" name="timeOut" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">工作时长</label>
                    <div class="layui-input-block">
                        <input type="text" name="workingTimeText" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">情况说明</label>
                    <div class="layui-input-block">
                        <input type="text" name="note" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">考勤状态</label>
                    <div class="layui-input-block">
                        <input type="text" name="stateText" class="layui-input" readonly>
                    </div>
                </div>
            </form>
        </div>
    </div>
</script>
<script src="/layui/layui.js"></script>
<script src="/js/common.js"></script>
<script>
    layui.config({
        base: '/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'form', 'laydate'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var admin = layui.admin;
        var laydate = layui.laydate;
        var popupIndex;
        var projectId = getProjectId();

        table.render({
            elem: '#sign-table',
            url: 'signs',
            where: {projectId},
            title: '',
            skin: 'line',
            cols: [[
                {type: 'checkbox'},
                {
                    title: '序号', width: 60, templet: function (d) {
                        return d.LAY_TABLE_INDEX + 1;
                    },
                },
                {field: 'userName', title: '人员姓名', width: 100},
                {field: 'locationIn', title: '签到位置', width: 180},
                {field: 'timeIn', title: '签到时间', width: 180},
                {field: 'locationOut', title: '签退位置', width: 180},
                {field: 'timeOut', title: '签退时间', width: 180},
                {field: 'workingTimeText', title: '工作时长', width: 150},
                {field: 'stateText', title: '考勤状态', width: 100},
                {field: 'note', title: '情况说明', width: 120},
                {fixed: 'right', title: '操作', toolbar: '#toolbar-operation', width: 240}
            ]],
            page: true
        });

        function showSignForm() {
            popupIndex = admin.popupRight({
                area: '360px',
                success: function () {
                    var formHtml = $('#sign-form').html();
                    $('#' + this.id).html(formHtml);
                    form.render();
                    laydate.render({
                        elem: '#sign-in-time'
                    });
                    laydate.render({
                        elem: '#sign-out-time'
                    });
                }
            });
            return popupIndex;
        }

        function closePopupForm() {
            if (popupIndex) {
                layer.close(popupIndex);
            }
        }

        function search() {
            table.reload('sign-table', {
                where: {
                    userName: $('#search-user-name').val(),
                    workingTimeGreaterThan: $('#search-working-time-greater-than').val(),
                    workingTimeLessThan: $('#search-working-time-less-than').val(),
                    state: $('#search-state').val()
                },
                page: {
                    curr: 1
                }
            });
        }

        $(document).on('keypress', function (e) {
            if (e.which == 13) {
                search();
            }
        });

        $("#btn-search").on('click', function () {
            search();
        });

        $("#btn-export").on('click', function () {
            let userName = $('#search-user-name').val();
            let state = $('#search-state').val();
            let url = 'signs/export?userName=' + userName + '&state=' + state;
            window.open(url);
        });

        $("#btn-refresh").on('click', function () {
            table.reload('sign-table');
            layer.msg('表格已刷新');
        });

        $("#btn-batch-delete").on('click', function () {
            layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                var ids = [];
                var data = table.checkStatus('sign-table').data;
                for (let i in data) {
                    ids.push(data[i].id);
                }
                $.ajax({
                    url: 'signs/' + ids.toString(),
                    type: 'DELETE',
                    success: function (result) {
                        if (result.code == 200) {
                            layer.msg('删除成功');
                            table.reload('sign-table');
                        } else {
                            layer.msg('删除失败');
                        }
                    }
                });
            });
        });

        table.on('tool(sign-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                if (!isManagerOrAdmin()) {
                    layer.msg('只有经理和管理员才能删除');
                    return;
                }
                layer.confirm('真的删除该记录吗？', function (index) {
                    $.ajax({
                        url: 'signs/' + data.id,
                        type: 'DELETE',
                        success: function (result) {
                            if (result.code == 200) {
                                layer.msg('删除成功');
                                table.reload('sign-table');
                            } else {
                                layer.msg('删除失败');
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'location') {
                layer.open({
                    title: '签到签退位置',
                    type: 2,
                    content: 'sign-map.html?id=' + data.id,
                    area: ['960px', '680px'],
                    maxmin: true
                });
            } else if (obj.event === 'edit') {
                showSignForm();
                form.val('sign-form', data);
            } else if (obj.event === 'sign-out') {
                layer.confirm('确定强制签退吗？', function (index) {
                    $.ajax({
                        url: '/signs/force-sign-out',
                        type: 'POST',
                        data: {userId: data.userId},
                        success: function (result) {
                            if (result.code == 0) {
                                layer.msg(result.data);
                                table.reload('sign-table');
                            } else {
                                layer.msg(result.msg);
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>
</body>

</html>